package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingxiChongzhi;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 账单明细-充值
 */
@Repository
public class AppMingxiChongzhiDao extends BaseDao<AppMingxiChongzhi> {

    /**
     * 查询列表
     */
    public List<AppMingxiChongzhi> findList(Integer userid, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_chongzhi where userid = ? ");
        paramList.add(userid);
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppMingxiChongzhi.class,splitPage);
    }

    public List<AppMingxiChongzhi> findList(Map<String, Object> params, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_chongzhi where ordstatus='01' ");
        if (StringEx.isNotNull(params.get("nick"))) {
            sbStr.append(" and nick like ? ");
            paramList.add("%" + params.get("nick") + "%");
        }
        if (StringEx.isNotNull(params.get("phone"))) {
            sbStr.append(" and phone like ? ");
            paramList.add("%" + params.get("phone") + "%");
        }
        if (StringEx.isNotNull(params.get("userid"))) {
            sbStr.append(" and userid = ? ");
            paramList.add(params.get("userid"));
        }
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            sbStr.append(" and userid in ( ");
            sbStr.append(params.get("userids"));
            sbStr.append(" ) ");
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppMingxiChongzhi.class,splitPage);
    }

    public List<AppMingxiChongzhi> findList1(Map<String, Object> params, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_chongzhi where 1=1 ");
        if (StringEx.isNotNull(params.get("nick"))) {
            sbStr.append(" and nick like ? ");
            paramList.add("%" + params.get("nick") + "%");
        }
        if (StringEx.isNotNull(params.get("phone"))) {
            sbStr.append(" and phone like ? ");
            paramList.add("%" + params.get("phone") + "%");
        }
        if (StringEx.isNotNull(params.get("userid"))) {
            sbStr.append(" and userid = ? ");
            paramList.add(params.get("userid"));
        }
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            sbStr.append(" and userid in ( ");
            sbStr.append(params.get("userids"));
            sbStr.append(" ) ");
        }
        if (StringEx.isNotNull(params.get("ordernum"))) {
            sbStr.append(" and ordernum like ? ");
            paramList.add("%" + params.get("ordernum") + "%");
        }
        if (StringEx.isNotNull(params.get("leixing"))) {
            sbStr.append(" and leixing = ? ");
            paramList.add(params.get("leixing"));
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppMingxiChongzhi.class,splitPage);
    }

    public List<AppMingxiChongzhi> total(Map<String, Object> params) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder(" select * from app_mingxi_chongzhi where 1=1 ");

//        if(params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
//            sbStr.append(" and to_days(createtime) = to_days(now())");
//        }
        if(params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
            sbStr.append(" and createtime like ? " );
            paramList.add("%" + params.get("today") + "%");
        }
        if(params.containsKey("alltoday") && StringEx.isNotNull(params.get("alltoday"))) {
            sbStr.append(" and createtime <= ? " );
            paramList.add(params.get("alltoday"));
        }

        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppMingxiChongzhi.class);
    }

    public List<AppMingxiChongzhi> findListSum(Map<String, Object> params) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_chongzhi where 1=1 ");
        if (StringEx.isNotNull(params.get("nick"))) {
            sbStr.append(" and nick like ? ");
            paramList.add("%" + params.get("nick") + "%");
        }
        if (StringEx.isNotNull(params.get("phone"))) {
            sbStr.append(" and phone like ? ");
            paramList.add("%" + params.get("phone") + "%");
        }
        if (StringEx.isNotNull(params.get("userid"))) {
            sbStr.append(" and userid = ? ");
            paramList.add(params.get("userid"));
        }
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            sbStr.append(" and userid in ( ");
            sbStr.append(params.get("userids"));
            sbStr.append(" ) ");
        }
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppMingxiChongzhi.class);
    }


    /**
     * 详情
     */
    public AppMingxiChongzhi get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_chongzhi where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppMingxiChongzhi.class);
    }


    /**
     * 根据订单号查询充值详情
     */
    public AppMingxiChongzhi findByOrdernum(String ordernum) {
        if (StringEx.isNullOrLing(ordernum)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_chongzhi where 1=1");
        sbStr.append(" and ordernum = ? ");
        paramList.add(ordernum);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppMingxiChongzhi.class);
    }

    /**
     * 根据苹果内购订单号查询充值详情
     */
    public AppMingxiChongzhi findByCustorderno(String custorderno) {
        if (StringEx.isNullOrLing(custorderno)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mingxi_chongzhi where 1=1");
        sbStr.append(" and custorderno = ? ");
        paramList.add(custorderno);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppMingxiChongzhi.class);
    }


    /**
     * 添加/修改
     */
    public void save(AppMingxiChongzhi haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }


    /**
     * 获取单个用户的充值钻石数量
     * @return
     */
    public List<AppMingxiChongzhi> selectUserAll(Integer id) {
        StringBuilder str = new StringBuilder();
        List<Object> paramList = new ArrayList<Object>();
        str.append("select * from app_mingxi_chongzhi where userid = ?");
        paramList.add(id);
        String sql = str.toString();
        return this.queryList(sql,paramList,AppMingxiChongzhi.class);
    }

    /*
    根据订单ID 修改充值结果
     */
    public void updateByCustOrderNo(String ordstatus, String custOrderNo,Integer status,String time) {
        StringBuilder str = new StringBuilder();
        str.append("update app_mingxi_chongzhi set ordstatus = ? ");
        List l = new ArrayList();
        l.add(ordstatus);
        str.append(" , status = ? ");
        l.add(status);
        str.append(" , paytime = ?");
        l.add(time);
        str.append(" ,  updatetime = current_timestamp");
        str.append(" where custorderno = ?");
        l.add(custOrderNo);
        String sql = str.toString();
        this.update(sql,l);
    }

    /*
    根据订单ID 修改充值结果 成功或者失败
     */
    public void updateByCustOrderNoSuccessOrFailure(String ordStatus,String custOrderNo,Integer status,String time) {
        StringBuilder str = new StringBuilder();
        str.append("update app_mingxi_chongzhi set ");
        List l = new ArrayList();
        str.append(" ordstatus = ? ");
        l.add(ordStatus);
        str.append(" , status = ? ");
        l.add(status);
        str.append(" , paytime = ?");
        l.add(time);
        str.append(" ,  updatetime = current_timestamp");
        str.append(" where custorderno = ?");
        l.add(custOrderNo);
        String sql = str.toString();
        this.update(sql,l);
    }

    /**
     * 通过订单号查询用户ID
     * @param custOrderNo
     * @return
     */
    public AppMingxiChongzhi selectByCustOrderNo(String custOrderNo) {
        StringBuilder str = new StringBuilder();
        List l = new ArrayList();
        str.append("select * from app_mingxi_chongzhi where custorderno = ?");
        l.add(custOrderNo);
        String sql = str.toString();
        return this.queryObj(sql,l, AppMingxiChongzhi.class);
    }

    /**
     * 查询充值记录列表
     * @param userId
     * @param splitPageBean
     * @return
     */
    public List<AppMingxiChongzhi> selectRechargeList(Integer userId, SplitPageBean splitPageBean) {
        StringBuilder str = new StringBuilder();
        List l = new ArrayList();
        str.append("select money,createtime,goumainum,orderDesc,ordernum,status,buscontent from app_mingxi_chongzhi where userid = ?");
        l.add(userId);
        str.append(" order by createtime desc");
        String sql = str.toString();
        return this.queryList(sql,l,AppMingxiChongzhi.class);

    }

    public AppMingxiChongzhi selectRecharge(String ordernum) {
        StringBuilder str = new StringBuilder();
        List l = new ArrayList();
        str.append("select createtime,orderDesc,goumainum,money,paytime from app_mingxi_chongzhi where ordernum = ?");
        l.add(ordernum);
        String sql = str.toString();
        return this.queryObj(sql,l,AppMingxiChongzhi.class);
    }
}
